package com.example.sunmiv1.room

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log
import com.example.sunmiv1.room.entity.UserVipEntity
import io.realm.Realm
import io.realm.RealmConfiguration
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean


object BaseApp{
    private lateinit var mApp:Application
    private val TAG=javaClass.simpleName
    fun isForeground()=mHashForeground
    private var mHashForeground=AtomicBoolean(false)
    fun onCreate(application: Application) {
        mApp=application
        Realm.init(application);
        val config = RealmConfiguration.Builder()
            .name("myrealm.realm")//数据库名称
            .build();
        Realm.setDefaultConfiguration(config)
        /*GlobalScope.launch(Dispatchers.IO){
            UserVipDaoManager.add(UserVipEntity().also {
                it.init("test1","test2","test3","test4","test5","test6","test7","test8")
            })
            UserVipDaoManager.getAll()
            UserVipDaoManager.findUserId("test1")
        }*/
        mApp.registerActivityLifecycleCallbacks(object :Application.ActivityLifecycleCallbacks{
            override fun onActivityCreated(p0: Activity, p1: Bundle?) {
                Log.e(TAG, "onActivityCreated: $p0")
                mCallMap.forEach {
                    it.value?.onActivityCreated(p0,p1)
                }
            }

            override fun onActivityStarted(p0: Activity) {
                Log.e(TAG, "onActivityStarted: $p0")
                mCallMap.forEach {
                    it.value?.onActivityStarted(p0)
                }
            }

            override fun onActivityResumed(p0: Activity) {

            }

            override fun onActivityPaused(p0: Activity) {
                Log.e(TAG, "oonActivityPaused: $p0")

            }

            override fun onActivityStopped(p0: Activity) {

            }

            override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
                Log.e(TAG, "onActivitySaveInstanceState: $p0")
                mCallMap.forEach {
                    it.value?.onActivitySaveInstanceState(p0,p1)
                }
            }

            override fun onActivityDestroyed(p0: Activity) {
                Log.e(TAG, "onActivitySaveInstanceState: $p0")
                mCallMap.forEach {
                    it.value?.onActivityDestroyed(p0)
                }
            }

        })
    }

    fun getInstall()= mApp
    private val mCoroutineScope by lazy {
        CoroutineScope(Dispatchers.IO)
    }
    private val mCallMap= hashMapOf<String,Application.ActivityLifecycleCallbacks?>()
    fun setCall(tag:String,call:Application.ActivityLifecycleCallbacks?){
        mCallMap[tag]=call
    }
}